import { createSlice } from "@reduxjs/toolkit";
import {
    getCourseListAPI,
    getCourseCondition,
    getPartListAPI,
    getInfoAPI,
    getStudentList,
    getTestsAPI,
    getVideoListAPI,
} from '@/apis/teacher.js'

const teacherStore = createSlice({
    name:'teacher',
    initialState:{
        courseList:[],
        courseCondition:{},
        partList:[],
        info:{},
        studentList:[],
        tests:[],
        videoList:[]
    },
    reducers:{
        setCourseList(state,action){
            state.courseList = action.payload;
        },
        setCourseCondition(state,action){
            state.courseCondition = action.payload;
        },
        setPartList(state,action){
            state.partList = action.payload;
        },
        setInfo(state,action){
            state.info = action.payload;
        },
        setStudentList(state,action){
            state.studentList = action.payload;
        },
        setTests(state,action){
            state.tests = action.payload;
        },
        setVideoList(state,action){
            state.videoList = action.payload;
        }
    }
})

const {
    setCourseCondition,
    setCourseList,
    setInfo,
    setPartList,
    setStudentList,
    setTests,
    setVideoList,
} = teacherStore.actions;

const teacherReducer = teacherStore.reducer;

const fetchGetCourseCondition = (class_id,token) => {
    return async function(dispatch) {
        const res = await getCourseCondition(class_id,token);
        console.log("get coursecondition",res.data);
        dispatch(setCourseCondition(res.data));
    }
}

const fetchGetCourseList = (token) => {
    return async function(dispatch) {
        const res = await getCourseListAPI(token);
        console.log("getCourseList",res.data);
        dispatch(setCourseList(res.data.map((ele,index)=>({...ele,id:index}))));
    }
}

const fetchInfo = (token) => {
    return async function(dispatch) {
        const res = await getInfoAPI(token);
        console.log("getInfoAPI",res.data);
        dispatch(setInfo(res.data));
    }
}

const fetchGetPartList = (class_id,token) => {
    return async function(dispatch) {
        const res = await getPartListAPI(class_id,token);
        console.log("get getPartListAPI",res.data);
        dispatch(setPartList(res.data.map((ele,index)=>({...ele,id:index}))));
    }
}

const fetchGetStudentList = (class_id,token) => {
    return async function(dispatch) {
        const res = await getStudentList(class_id,token);
        console.log("get getStudentList",res.data);
        dispatch(setStudentList(res.data.map((ele,index)=>({...ele,id:index}))));
    }
}

const fetchGetTests = (class_id,token) => {
    return async function(dispatch) {
        const res = await getTestsAPI(class_id,token);
        console.log("get TestsAPI",res.data);
        dispatch(setTests(res.data.map((ele,index)=>({...ele,id:index}))));
    }
}

const fetchGetVideoList = (class_id,token) => {
    return async function(dispatch) {
        const res = await getVideoListAPI(class_id,token);
        console.log("getVideoListAPI",res.data);
        dispatch(setVideoList(res.data.map((ele,index)=>({...ele,id:index}))));
    }
}


export {
    setCourseCondition,
    setCourseList,
    setInfo,
    setPartList,
    setStudentList,
    setTests,
    setVideoList,
    fetchGetCourseCondition,
    fetchGetCourseList,
    fetchGetPartList,
    fetchGetStudentList,
    fetchGetTests,
    fetchGetVideoList,
    fetchInfo
}

export default teacherReducer;